package com.changgou.search.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitMQConfig {

    //交换机
    public static final String GOODS_UP_EXCHANGE ="good_up_exchange";
    public static final String GOODS_DOWN_EXCHANGE ="goods_down_exchange";

    //队列
    public static final String AD_UPDATE_QUEUE = "ad_update_queue";
    public static final String SEARCH_ADD_QUEUE = "search_add_queue";
    public static final String SEARCH_DELETE_QUEUE = "search_delete_queue";

    /**
     * 添加的交换机
     * @return
     */
    @Bean("good_up_exchange")
    public Exchange good_up_exchange(){
        return ExchangeBuilder.topicExchange(GOODS_UP_EXCHANGE).durable(true).build();
    }
    /**
     * 删除的交换机
     */
    @Bean("goods_down_exchange")
    public Exchange goods_down_exchange(){
        return ExchangeBuilder.topicExchange(GOODS_DOWN_EXCHANGE).durable(true).build();
    }

    /**
     * 队列
     * @return
     */
    @Bean("ad_update_queue")
    public Queue ad_update_queue(){
        return QueueBuilder.durable(AD_UPDATE_QUEUE).build();
    }
    @Bean("search_add_queue")
    public Queue search_add_queue(){
        return QueueBuilder.durable(SEARCH_ADD_QUEUE).build();
    }
    //删除的交换机
    @Bean("search_delete_queue")
    public Queue search_delete_queue(){
        return QueueBuilder.durable(SEARCH_DELETE_QUEUE).build();
    }
    /*
        1. 知道哪个队列
        2. 知道哪个交换机
        3. routing key
     */
    @Bean
    public Binding good_up_exchange_binding(@Qualifier("search_add_queue") Queue queue, @Qualifier("good_up_exchange") Exchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with("").noargs();
    }

    @Bean
    public Binding goods_down_exchange_binding(@Qualifier("search_delete_queue") Queue queue, @Qualifier("goods_down_exchange") Exchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with("").noargs();
    }
}
